Skip to content

New locking for frame status#1627

Merged
DawidWesierski4 merged 6 commits into
mainfrom
new_locking_for_frame_status
Jul 15, 2026
Merged

New locking for frame status#1627
DawidWesierski4 merged 6 commits into
mainfrom
new_locking_for_frame_status

Conversation

@DawidWesierski4

Copy link
Copy Markdown
Collaborator

No description provided.

@DawidWesierski4 DawidWesierski4 force-pushed the new_locking_for_frame_status branch 4 times, most recently from 699c49c to dedc1d4 Compare June 25, 2026 15:11
@DawidWesierski4 DawidWesierski4 force-pushed the new_locking_for_frame_status branch from dedc1d4 to 873f59e Compare July 14, 2026 07:10
@Sakoram

Sakoram commented Jul 14, 2026

Copy link
Copy Markdown
Collaborator

We are mixing GCC buildins with C11 _Atomic (from stdatomic.h) in this project.
Please stick with C11 _Atomic, declare framebuff_sequence_number also as _Atomic, and stick with C11 functions like atomic_fetch_add_explicit instead of __atomic_fetch_add.

Comment thread lib/src/st2110/pipeline/st30_pipeline_rx.c
@Sakoram

Sakoram commented Jul 14, 2026

Copy link
Copy Markdown
Collaborator

please write somewhere in docs, possibly in design.md our assumption of thread safety because it will work only if frame_get and frame_put are called from the same thread and are synchronous. Also our assumption is that all functions called from lower layers like frame_ready are synchronous.

@DawidWesierski4 DawidWesierski4 force-pushed the new_locking_for_frame_status branch from 873f59e to 132e360 Compare July 14, 2026 13:33

@Sakoram Sakoram left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great job.
This looks much better then current mutexes

@DawidWesierski4 DawidWesierski4 force-pushed the new_locking_for_frame_status branch from 132e360 to 75d256c Compare July 15, 2026 07:52
Remove the pthread_mutex_t from all ST2110 media pipeline contexts (ST20,
ST22, ST30, and ST40) and replace the internal framebuffer state tracking
with a lock-free design using C11 atomics. This eliminates mutex contention
between application and transport threads, enabling safe concurrent
access to the framebuffer ring.

Convert `stat` in all 8 pipeline frame structs to `_Atomic uint32_t`.
Stat is now acting as our lock-free state machine for the framebuffer
ring, with atomic compare-and-swap (CAS) operations used to transition
between states.

Ensure thread-safe TX sequence number generation via atomic
`__atomic_fetch_add` on `framebuff_sequence_number`.
Remove all `pthread_mutex_t lock` usage, initialization, and
destruction across lib/src/st2110/pipeline/.

Use atomic CAS for pipeline framebuffer status transitions
Integrate upstream stress tests from commit 5fdceaa7 and add new
deterministic concurrency tests for ST20, ST22, ST30, and ST40
pipelines. These tests verify the new lock-free implementation by
simulating high-contention scenarios across multiple production and
consumption threads.

- Update pipeline harnesses to remove pthread_mutex, aligning with
  the library's move to lock-free buffers.
- Add MultiProducerNoSpuriousClaimFailure and similar test cases
  to verify thread safety under stress.
- Update Meson build to include the new test suites.

Signed-off-by: Dawid Wesierski <dawid.wesierski@intel.com>
Replace GCC __atomic_* builtins with <stdatomic.h> equivalents
(atomic_load_explicit / atomic_store_explicit / atomic_fetch_add_explicit
/ atomic_compare_exchange_*_explicit) and declare the tx framebuff
sequence-number counters _Atomic. Ordering and semantics are unchanged;
this aligns the pipeline with the C11 lock-free model already used by
mt_handle_guard.h.

Signed-off-by: Dawid Wesierski <dawid.wesierski@intel.com>
The lock-free framebuffer status protocol is correct only when get_frame
and put_frame run on one thread and synchronously, and when transport
callbacks (frame_ready, packet_convert, frame_done, query_ext_frame) run
synchronously within their single owning tasklet. Record these invariants
under section 6.3 so callers do not assume the get/put APIs are
re-entrant or safe for concurrent multi-threaded use.

Signed-off-by: Dawid Wesierski <dawid.wesierski@intel.com>
The TX blocking get_frame waited on block_wake_cond with a single
cond_timedwait and no predicate, so a wake signalled between the failed
frame claim and entering the wait was lost (the consumer then blocked
the full timeout despite an available frame), and a spurious wakeup
could return NULL before the timeout elapsed. Mirror the RX side: add
block_wake_pending, set it in tx_*_block_wake, and loop on the predicate
until a frame is signalled, the handle is destroying, or the wait times
out.

Signed-off-by: Dawid Wesierski <dawid.wesierski@intel.com>
Should be impossible to reach normally but test a scneario where
wakeup is called spourously.

Tests: 47e10a5
@DawidWesierski4 DawidWesierski4 enabled auto-merge (rebase) July 15, 2026 10:52
@DawidWesierski4 DawidWesierski4 merged commit 950bb49 into main Jul 15, 2026
27 checks passed
@DawidWesierski4 DawidWesierski4 deleted the new_locking_for_frame_status branch July 15, 2026 11:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants